package com.junci.mtest.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Locale;

/**
 * 日期操作工具类.
 */
public class DateUtil {

    /**
     * 将时间格式化为显示的时间
     *
     * @param date 要格式化的时间，毫秒单位
     * @return 格式化后的格式
     */
    public static String formatDate(String date) {

        long notepadTime = Long.parseLong(date);
        Date notepadDate = new Date(notepadTime);
        int notepadYear = notepadDate.getYear();
        int notepadMonth = notepadDate.getMonth();
        int notepadDay = notepadDate.getDate();
        int notepadHours = notepadDate.getHours();
        int notepadMinutes = notepadDate.getMinutes();
//        int notepadSeconds = notepadDate.getSeconds();


        long currentTime = System.currentTimeMillis();
        Date currentDate = new Date(currentTime);
        int currentYear = currentDate.getYear();
        int currentMouth = currentDate.getMonth();
        int currentDay = currentDate.getDate();
//        int currentHours = currentDate.getHours();
//        int currentMinutes = currentDate.getMinutes();
//        int currentSeconds = currentDate.getSeconds();

//        Date flagDate = new Date();
//        SimpleDateFormat format = new SimpleDateFormat("yyyy_MM_dd HH:mm:ss");
//        Calendar calendar = format.getCalendar();
//        Calendar calendar2 = format.getCalendar();
//        calendar.set(currentYear,currentMouth,currentDay,0,0,0);
//        calendar2.set(currentYear, currentMouth, currentDay, currentHours, currentMinutes, currentSeconds);
//        calendar.getTimeInMillis();


        if (currentYear == notepadYear) {
            // 今年保存的

            if (notepadMonth == currentMouth) {
                // 月份相同,比较day
                if (currentDay - notepadDay == 1) {
                    // 昨天
                    date = "昨天";
                } else if (currentDay - notepadDay == 2) {
                    // 前天
                    date = "前天";
                } else if (currentDay == notepadDay) {
                    // 今天
                    // 对时间的分做双位显示处理
                    String twoNumNotepadMinutes = getTwoNumDate(notepadMinutes);
                    date = notepadHours + "：" + twoNumNotepadMinutes;
                } else {
                    // 对时间的日做双位显示处理
//                    String twoNumNotepadDay = getTwoNumDate(notepadDay);
                    // 以前
                    date = (notepadMonth + 1) + "/" + notepadDay;
                }

            } else if (currentMouth - notepadMonth == 1) {
                // 上个月保存的。判断现在是否是第一天，如果是，判断保存的是否是最后一天，或者倒数第二天
//                currentDate.setDate(currentDay - 1);
//                int cm = currentDate.getMonth();

                if (currentDay == 1) {
                    // 是这个月的第一天，判断保存的是否是最后一两天
                    notepadDate.setDate(notepadDay + 1);
                    int nm = notepadDate.getMonth();
                    if (notepadMonth != nm) {
                        // 上个月最后一天
                        // 昨天的，显示昨天
                        date = "昨天";
                    } else {
                        // 不是上个月最后一天，判断是否是上个月倒数第二天
                        notepadDate.setDate(notepadDay + 2);
                        nm = notepadDate.getMonth();
                        if (notepadMonth != nm) {
                            // 是上个月倒数第二天
                            date = "前天";
                        } else {
                            // 对时间的日做双位显示处理
//                            String twoNumNotepadDay = getTwoNumDate(notepadDay);
                            // 以前的，显示月日
                            date = (notepadMonth + 1) + "/" + notepadDay;
                        }
                    }
                } else if (currentDay == 2) {
                    // 不是这个月的第一天，是这个月的第二天，判断保存日期是否是上个月的最后一天
                    notepadDate.setDate(notepadDay + 1);
                    int nm = notepadDate.getMonth();
                    if (notepadMonth != nm) {
                        // 上个月最后一天
                        // 显示前天
                        date = "昨天";
                        ;

                    } else {
                        // 对时间的日做双位显示处理
//                        String twoNumNotepadDay = getTwoNumDate(notepadDay);
                        // 以前的，显示月日
                        date = (notepadMonth + 1) + "/" + notepadDay;
                    }
                }

            }

        } else {
            // 将年份做取 两位 处理
            String twoYear = notepadYear + "";
            S("  twoYear:" + twoYear + "  date:" + date);
            twoYear = twoYear.substring(1);

            // 其他年份保存的记事，显示年月日
            date = twoYear + "/" + (notepadMonth + 1) + "/" + notepadDay;
            S("  twoYear:" + twoYear + "  date:" + date);
        }

        return date;
    }

    /**
     * 将一位数的时间装换为两位数
     *
     * @param date
     * @return
     */
    private static String getTwoNumDate(int date) {
        String twoDate = date + "";
        if (twoDate.toCharArray().length < 2) {
            twoDate = "0" + twoDate;
        }
        return twoDate;
    }

    private static void S(String s) {

    }


    private static final String FORMAT = "yyyy-MM-dd HH:mm:ss";
    private static final SimpleDateFormat datetimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static final SimpleDateFormat datetimeFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    private static final SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");

    public static Date str2Date(String str) {
        return str2Date(str, null);
    }

    /**
     * 字符串转时间
     *
     * @param str
     * @param format
     * @return
     */
    public static Date str2Date(String str, String format) {
        if (str == null || str.length() == 0) {
            return null;
        }
        if (format == null || format.length() == 0) {
            format = FORMAT;
        }
        Date date = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            date = sdf.parse(str);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;

    }

    public static Calendar str2Calendar(String str) {
        return str2Calendar(str, null);

    }

    public static Calendar str2Calendar(String str, String format) {
        Date date = str2Date(str, format);
        if (date == null) {
            return null;
        }
        Calendar c = Calendar.getInstance();
        c.setTime(date);

        return c;

    }

    public static String date2Str(Calendar c) {// yyyy-MM-dd HH:mm:ss
        return date2Str(c, null);
    }

    public static String date2Str(Calendar c, String format) {
        if (c == null) {
            return null;
        }
        return date2Str(c.getTime(), format);
    }

    public static String date2Str(Date d) {// yyyy-MM-dd HH:mm:ss
        return date2Str(d, null);
    }

    public static String date2Str(Date d, String format) {// yyyy-MM-dd HH:mm:ss
        if (d == null) {
            return null;
        }
        if (format == null || format.length() == 0) {
            format = FORMAT;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        String s = sdf.format(d);
        return s;
    }

    public static String getCurDateStr() {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        return c.get(Calendar.YEAR) + "-" + (c.get(Calendar.MONTH) + 1) + "-" + c.get(Calendar.DAY_OF_MONTH) + "-"
                + c.get(Calendar.HOUR_OF_DAY) + ":" + c.get(Calendar.MINUTE) + ":" + c.get(Calendar.SECOND);
    }

    /**
     * 根据当前日期获得是星期几
     * time=yyyy-MM-dd
     * @return
     */
    public static String getWeek(String time) {
        String Week = "";
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        try {
            c.setTime(format.parse(time));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        int wek=c.get(Calendar.DAY_OF_WEEK);

        if (wek == 1) {
            Week += "周日";
        }
        if (wek == 2) {
            Week += "周一";
        }
        if (wek == 3) {
            Week += "周二";
        }
        if (wek == 4) {
            Week += "周三";
        }
        if (wek == 5) {
            Week += "周四";
        }
        if (wek == 6) {
            Week += "周五";
        }
        if (wek == 7) {
            Week += "周六";
        }
        return Week;
    }

    /**
     * 获取固定间隔时刻集合
     * @param start 开始时间
     * @param end 结束时间
     * @param interval 时间间隔(单位：分钟)
     * @return
     */
    public static List<String> getIntervalTimeList(String start,String end,int interval) throws ParseException {
        Date startDate =datetimeFormat.parse(start);
        Date endDate = datetimeFormat.parse(end);
        List<String> list = new ArrayList<>();
        while(startDate.getTime()<=endDate.getTime()){
            list.add(datetimeFormat.format(startDate));
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(startDate);
            calendar.add(Calendar.MINUTE,interval);
            if(calendar.getTime().getTime()>endDate.getTime()){
                if(!startDate.equals(endDate)){
                    list.add(datetimeFormat.format(endDate));
                }
                startDate = calendar.getTime();
            }else{
                startDate = calendar.getTime();
            }

        }
        return list;
    }

    /**
     * 获得当前时间最接近的刻度时间
     *
     * @return
     */
    public static String GetNearest15MinutesDateByCurrentDate(){
        Calendar calendar = Calendar.getInstance();//获取当前时间
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        int minute = calendar.get(Calendar.MINUTE);// 得到分钟
        int count = 0;
        if (minute != 0 && minute != 15 && minute != 30 && minute != 45) { //时间不为00，15，30，45的情况
            for (int i = minute; i >= 0; i--) { //判断距离最近的00，15，30，45的分钟数
                if (i % 15 == 0) {
                    break;
                } else {
                    count++;
                }
            }
        }
        calendar.add(Calendar.MINUTE, -count); //获取最近的分钟
        int year = calendar.get(Calendar.YEAR); //得到年
        int month = calendar.get(Calendar.MONTH) + 1; //得到月
        int day = calendar.get(Calendar.DAY_OF_MONTH);// 得到天
        int hour = calendar.get(Calendar.HOUR_OF_DAY);// 得到小时
        int min = calendar.get(Calendar.MINUTE);// 得到分钟
        StringBuffer datebuffer = new StringBuffer();
        datebuffer.append(year);
        datebuffer.append("-");
        if(month <10){
            datebuffer.append("0"+month+"-");
        }else{
            datebuffer.append(month+"-");
        }
        if(day<10){
            datebuffer.append("0"+day+" ");
        }else{
            datebuffer.append(day+" ");
        }
        if(hour <10){
            datebuffer.append("0"+hour+":");
        }else{
            datebuffer.append(hour+":");
        }
        if(min<10){
            datebuffer.append("0"+min+":");
        }else{
            datebuffer.append(min+":");
        }
        datebuffer.append("00");
//        System.out.println(datebuffer.toString());
        return datebuffer.toString();
    }



    /**
     * 获得当前日期的字符串格式
     *
     * @param format
     * @return
     */
    public static String getCurDateStr(String format) {
        Calendar c = Calendar.getInstance();
        return date2Str(c, format);
    }

    /**
     * 格式到秒
     *
     * @param time
     * @return
     */
    public static String getMillon(long time) {
        return new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(time);

    }
    /**
     * 格式到小时
     *
     * @param time
     * @return
     */
    public static String getHour(long time) {
        return new SimpleDateFormat("MM月dd日HH时").format(time);

    }

    /**
     * 格式到天
     *
     * @param time
     * @return
     */
    public static String getDay(long time) {
        return new SimpleDateFormat("yyyy-MM-dd-HH-mm").format(time);
    }
    /**
     * 格式到天
     *
     * @param time
     * @return
     */
    public static String getDate(long time) {
        return new SimpleDateFormat("yyyy-MM-dd").format(time);
    }
    /**
     * 格式到年
     *
     * @param time
     * @return
     */
    public static String getYear(long time) {
        return new SimpleDateFormat("yyyy年").format(time);
    }
    /**
     * 格式到月
     *
     * @param time
     * @return
     */
    public static String getMonth(long time) {
        return new SimpleDateFormat("yyyy年MM月").format(time);
    }

    /**
     * 格式到毫秒
     *
     * @param time
     * @return
     */
    public static String getSMillon(long time) {
        return new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss-SSS").format(time);

    }

    /**
     * 字符串转换到时间格式
     *
     * @param dateStr   需要转换的字符串
     * @param formatStr 需要格式的目标字符串 举例 yyyy-MM-dd
     * @return Date 返回转换后的时间
     * @throws ParseException 转换异常
     */
    public static Date StringToDate(String dateStr, String formatStr) {
        DateFormat sdf = new SimpleDateFormat(formatStr);
        Date date = null;
        try {
            date = sdf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 转化时间输入时间与当前时间的间隔
     *
     * @param timestamp
     * @return
     */
    public static String converTime(long timestamp) {

        long currentSeconds = System.currentTimeMillis()/1000;
        long timeGap = currentSeconds - timestamp;// 与现在时间相差秒数
        String timeStr = null;
        if (timeGap > 24 * 60 * 60) {// 1天以上
            timeStr = timeGap / (24 * 60 * 60) + "天前";
        } else if (timeGap > 60 * 60) {// 1小时-24小时
            timeStr = timeGap / (60 * 60) + "小时前";
        } else if (timeGap > 60) {// 1分钟-59分钟
            timeStr = timeGap / 60 + "分钟前";
        } else {// 1秒钟-59秒钟
            timeStr = "刚刚";
        }
        return timeStr;
    }
    /**
     * 转化时间输入时间与当前时间的间隔
     *
     * @param timestamp
     * @return
     */
    public static int converTimeM(long timestamp) {
        long currentSeconds = System.currentTimeMillis() / 1000;
        long timeGap = currentSeconds - timestamp;// 与现在时间相差秒数
        if (timeGap > 24 * 60 * 60) {// 1天以上
            return 70;
        } else if (timeGap > 60 * 60) {// 1小时-24小时
            return 60;
        } else if (timeGap > 60) {// 1分钟-59分钟
            return (int) (timeGap/60);
        } else {// 1秒钟-59秒钟
            return 0;
        }

    }

    /**
     * 转化时间输入时间与当前时间的间隔
     *
     * @param timestamp
     * @return
     */
    public static long converTimeRL(long timestamp) {
        long currentSeconds = System.currentTimeMillis() / 1000;
        long timeGap = currentSeconds - (timestamp/1000);// 与现在时间相差秒数

        return timeGap;
    }

    /**
     * 把字符串转化为时间格式
     *
     * @param timestamp
     * @return
     */
    public static String getStandardTime(long timestamp) {
        SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日 HH:mm");
        Date date = new Date(timestamp * 1000);
        sdf.format(date);
        return sdf.format(date);
    }

    /**
     * 获得当前日期时间 日期时间格式yyyy-MM-dd HH:mm:ss
     *
     * @return
     */
    public static String currentDatetime() {
        return datetimeFormat.format(now());
    }
    public static String currentDatetimeuns() {
        return datetimeFormat1.format(now());
    }

    /**
     * 格式化日期时间 日期时间格式yyyy-MM-dd HH:mm:ss
     *
     * @return
     */
    public static String formatDatetime(Date date) {
        return datetimeFormat.format(date);
    }

    /**
     * 获得当前时间 时间格式HH:mm:ss
     *
     * @return
     */
    public static String currentTime() {
        return timeFormat.format(now());
    }

    /**
     * 格式化时间 时间格式HH:mm:ss
     *
     * @return
     */
    public static String formatTime(Date date) {
        return timeFormat.format(date);
    }

    /**
     * 获得当前时间的<code>java.util.Date</code>对象
     *
     * @return
     */
    public static Date now() {
        return new Date();
    }

    public static Calendar calendar() {
        Calendar cal = GregorianCalendar.getInstance(Locale.CHINESE);
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        return cal;
    }

    /**
     * 获得当前时间的毫秒数
     * <p>
     * 详见{@link System#currentTimeMillis()}
     *
     * @return
     */
    public static long millis() {
        return System.currentTimeMillis();
    }

    /**
     * 获得当前Chinese月份
     *
     * @return
     */
    public static int month() {
        return calendar().get(Calendar.MONTH) + 1;
    }

    /**
     * 获得月份中的第几天
     *
     * @return
     */
    public static int dayOfMonth() {
        return calendar().get(Calendar.DAY_OF_MONTH);
    }

    /**
     * 今天是星期的第几天
     *
     * @return
     */
    public static int dayOfWeek() {
        return calendar().get(Calendar.DAY_OF_WEEK);
    }

    /**
     * 今天是年中的第几天
     *
     * @return
     */
    public static int dayOfYear() {
        return calendar().get(Calendar.DAY_OF_YEAR);
    }

    /**
     * 判断原日期是否在目标日期之前
     *
     * @param src
     * @param dst
     * @return
     */
    public static boolean isBefore(Date src, Date dst) {
        return src.before(dst);
    }

    /**
     * 判断原日期是否在目标日期之后
     *
     * @param src
     * @param dst
     * @return
     */
    public static boolean isAfter(Date src, Date dst) {
        return src.after(dst);
    }

    /**
     * 判断两日期是否相同
     *
     * @param date1
     * @param date2
     * @return
     */
    public static boolean isEqual(Date date1, Date date2) {
        return date1.compareTo(date2) == 0;
    }

    /**
     * 判断某个日期是否在某个日期范围
     *
     * @param beginDate 日期范围开始
     * @param endDate   日期范围结束
     * @param src       需要判断的日期
     * @return
     */
    public static boolean between(Date beginDate, Date endDate, Date src) {
        return beginDate.before(src) && endDate.after(src);
    }

    /**
     * 获得当前月的最后一天
     * <p>
     * HH:mm:ss为0，毫秒为999
     *
     * @return
     */
    public static Date lastDayOfMonth() {
        Calendar cal = calendar();
        cal.set(Calendar.DAY_OF_MONTH, 0); // M月置零
        cal.set(Calendar.HOUR_OF_DAY, 0);// H置零
        cal.set(Calendar.MINUTE, 0);// m置零
        cal.set(Calendar.SECOND, 0);// s置零
        cal.set(Calendar.MILLISECOND, 0);// S置零
        cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) + 1);// 月份+1
        cal.set(Calendar.MILLISECOND, -1);// 毫秒-1
        return cal.getTime();
    }

    /**
     * 获得当前月的第一天
     * <p>
     * HH:mm:ss SS为零
     *
     * @return
     */
    public static Date firstDayOfMonth() {
        Calendar cal = calendar();
        cal.set(Calendar.DAY_OF_MONTH, 1); // M月置1
        cal.set(Calendar.HOUR_OF_DAY, 0);// H置零
        cal.set(Calendar.MINUTE, 0);// m置零
        cal.set(Calendar.SECOND, 0);// s置零
        cal.set(Calendar.MILLISECOND, 0);// S置零
        return cal.getTime();
    }

    private static Date weekDay(int week) {
        Calendar cal = calendar();
        cal.set(Calendar.DAY_OF_WEEK, week);
        return cal.getTime();
    }

    /**
     * 获得周五日期
     * <p>
     * 注：日历工厂方法{@link #calendar()}设置类每个星期的第一天为Monday，US等每星期第一天为sunday
     *
     * @return
     */
    public static Date friday() {
        return weekDay(Calendar.FRIDAY);
    }

    /**
     * 获得周六日期
     * <p>
     * 注：日历工厂方法{@link #calendar()}设置类每个星期的第一天为Monday，US等每星期第一天为sunday
     *
     * @return
     */
    public static Date saturday() {
        return weekDay(Calendar.SATURDAY);
    }

    /**
     * 获得周日日期 注：日历工厂方法{@link #calendar()}设置类每个星期的第一天为Monday，US等每星期第一天为sunday
     *
     * @return
     */
    public static Date sunday() {
        return weekDay(Calendar.SUNDAY);
    }

    /**
     * 将字符串日期时间转换成java.util.Date类型 日期时间格式yyyy-MM-dd HH:mm:ss
     *
     * @param datetime
     * @return
     */
    public static Date parseDatetime(String datetime) throws ParseException {
        return datetimeFormat.parse(datetime);
    }

    /**
     * 将字符串日期转换成java.util.Date类型 日期时间格式yyyy-MM-dd
     *
     * @param date
     * @return
     * @throws ParseException
     */
    public static Date parseDate(String date) throws ParseException {
        return dateFormat.parse(date);
    }

    /**
     * 将字符串日期转换成java.util.Date类型 时间格式 HH:mm:ss
     *
     * @param time
     * @return
     * @throws ParseException
     */
    public static Date parseTime(String time) throws ParseException {
        return timeFormat.parse(time);
    }

    /**
     * 根据自定义pattern将字符串日期转换成java.util.Date类型
     *
     * @param datetime
     * @param pattern
     * @return
     * @throws ParseException
     */
    public static Date parseDatetime(String datetime, String pattern) throws ParseException {
        SimpleDateFormat format = (SimpleDateFormat) datetimeFormat.clone();
        format.applyPattern(pattern);
        return format.parse(datetime);
    }

    /**
     * 把秒格式化为分种小时
     *
     * @param second
     * @return
     */
    public static String parseSecond(int second) {
        if (second >= 60) {
            return second / 60 + "分";
        } else if (second >= 60 * 60) {
            return second / 60 * 60 + "时";
        } else if (second >= 60 * 60 * 24) {
            return second / 60 * 60 + "天";
        } else {
            return second + "秒";
        }
    }

    /**
     * 将秒数转为时分秒
    * */
    public static String changeSecond(long second) {
        long h = 0;
        long d = 0;
        long s = 0;
        long temp = second % 3600;
        if (second > 3600) {
            h = second / 3600;
            if (temp != 0) {
                if (temp > 60) {
                    d = temp / 60;
                    if (temp % 60 != 0) {
                        s = temp % 60;
                    }
                } else {
                    s = temp;
                }
            }
        } else {
            d = second / 60;
            if (second % 60 != 0) {
                s = second % 60;
            }
        }

        return (h>=10?h:"0"+h) + ":" + (d>=10?d:"0"+d) + ":" + (s>=10?s:"0"+s) + "";
    }
    /**
     * 将秒数转为时分
     * */
    public static String changeSecondhm(long second) {
        long h = 0;
        long d = 0;
        long s = 0;
        long temp = second % 3600;
        if (second > 3600) {
            h = second / 3600;
            if (temp != 0) {
                if (temp > 60) {
                    d = temp / 60;
                    if (temp % 60 != 0) {
                        s = temp % 60;
                    }
                } else {
                    s = temp;
                }
            }
        } else {
            d = second / 60;
            if (second % 60 != 0) {
                s = second % 60;
            }
        }

        return (h>=10?h:"0"+h) + ":" + (d>=10?d:"0"+d);
    }

    /**
     * 比较时间大小
     *
     * @param begin
     * @param end
     * @return
     */
    public static int compareDate(String begin, String end) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm");
        try {
            Date beginDate = df.parse(begin);
            Date endDate = df.parse(end);
            if (beginDate.getTime() < endDate.getTime()) {
                return 1;
            } else if (beginDate.getTime() > endDate.getTime()) {
                return -1;
            } else {
                return 0;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return 0;
    }
    /*
     *计算time2减去time1的差值 差值只设置 几天 几个小时 或 几分钟
     * 根据差值返回多长之间前或多长时间后
     * */
    public static String getDistanceTime(long  time1,long time2 ) {
        long day = 0;
        long hour = 0;
        long min = 0;
        long sec = 0;
        long diff ;
        String flag;
        if(time1<time2) {
            diff = time2 - time1;
            flag="前";
        } else {
            diff = time1 - time2;
            flag="后";
        }
        day = diff / (24 * 60 * 60 * 1000);
        hour = (diff / (60 * 60 * 1000) - day * 24);
        min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
        sec = (diff/1000-day*24*60*60-hour*60*60-min*60);
        if(day!=0)return day+"天"+flag;
        if(hour!=0)return hour+"小时"+flag;
        if(min!=0)return min+"分钟"+flag;
        return "刚刚";
    }

    /**
     * 获得年份
     *
     * @param date
     * @return
     */
    public static int getYear(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c.get(Calendar.YEAR);
    }

    /**
     * 获得月份
     *
     * @param date
     * @return
     */
    public static int getMonth(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c.get(Calendar.MONTH) + 1;
    }

    /**
     * 获得星期几
     *
     * @param date
     * @return
     */
    public static int getWeek(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c.get(Calendar.WEEK_OF_YEAR);
    }

    /**
     * 获得日期
     *
     * @param date
     * @return
     */
    public static int getDay(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c.get(Calendar.DATE);
    }

    /**
     * 获得天数差
     *
     * @param begin
     * @param end
     * @return
     */
    public long getDayDiff(Date begin, Date end) {
        long day = 1;
        if (end.getTime() < begin.getTime()) {
            day = -1;
        } else if (end.getTime() == begin.getTime()) {
            day = 1;
        } else {
            day += (end.getTime() - begin.getTime()) / (24 * 60 * 60 * 1000);
        }
        return day;
    }

}
